<?php

namespace app\admin\controller;

use app\common\model\Posts as PostsModel;
use think\Controller;
use think\Request;

class Posts extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($currentPage = 1, $pageSize = 10, $category = 0)
    {
        $map['status'] = 1;
        if ($category != 0) {
            $map['category'] = $category;
        }
        $list = PostsModel::where($map)->order('update_time desc')->page($currentPage, $pageSize)->select();
        $postsCount = PostsModel::where($map)->count();
        $data = [
            'current_page' => $currentPage,
            'page_size' => $pageSize,
            'last_page' => ceil($postsCount / $pageSize),
            'total' => $postsCount,
            'data' => $list,
        ];
        return responseData('success', '获取列表成功', $data);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $formData = $request->param();
        $result = $this->validate(
            $formData,
            'app\admin\validate\Posts');

        if (true !== $result) {
            // 验证失败 输出错误信息
            return responseData('fail', '缺少参数', $result)->code(400);
        } else {
            $model = new PostsModel;
            $model->data([
                'title' => $formData['title'],
                'title_color' => $formData['title_color'],
                'content' => $formData['content'],
                'category' => $formData['category'],
                'description' => $formData['description'],
                'keywords' => $formData['keywords'],
                'thumb' => $formData['thumb'],
                'create_time' => date("y-m-d"),
                'update_time' => $formData['update_time'],
            ]);
            $id = $model->save();
            if ($id) {
                return responseData('success', '添加成功', $id);
            } else {
                return responseData('fail', '添加失败！', $id);
            }
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $post = PostsModel::get($id);
        return responseData('success', '获取文章成功', $post);

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $formData = $request->param();
        $result = $this->validate(
            $formData,
            'app\admin\validate\Posts');

        if (true !== $result) {
            // 验证失败 输出错误信息
            return responseData('fail', '缺少参数', $result)->code(400);
        } else {
            $model = new PostsModel;
            $id = $model->save([
                'title' => $formData['title'],
                'title_color' => $formData['title_color'],
                'content' => $formData['content'],
                'category' => $formData['category'],
                'description' => $formData['description'],
                'keywords' => $formData['keywords'],
                'thumb' => $formData['thumb'],
                'update_time' => $formData['update_time'],
            ], ['id' => $id]);
            if ($id) {
                return responseData('success', '更新成功', $id);
            } else {
                return responseData('fail', '更新失败！', $id);
            }
        }

    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result = PostsModel::where('id=' . $id)->delete();
        if ($result) {
            return responseData('success', '删除成功', $result);
        }
    }
}
